package com.smartdio.mindtrain.images;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.smartdio.mindtrain.Mind;
import com.smartdio.mindtrain.core.Train;
import com.smartdio.mindtrain.core.TrainMan;


/**
 * 负责绘画图片的对象
 * 
 * @author smart
 * 
 */
public class ImagePainter {

	// private String imagePath = "E:\\MyFile\\My
	// Pictures\\icons\\Classic_Car_Icons_by_cemagraphics\\Childhood_Dream\\128.png";
	private TrainMan trainMan;

	private boolean test = false;

	private int width, height;

	private int[][] grids = new int[][] { { 2, 2 }, { 3, 2 }, { 3, 3 },
			{ 5, 4 }, { 6, 5 } ,{8,5}};

	public ImagePainter() {
		super();
	}

	/**
	 * @return the test
	 */
	public boolean isTest() {
		return test;
	}

	/**
	 * @param test
	 *            the test to set
	 */
	public void setTest(boolean test) {
		this.test = test;
	}

	/**
	 * 
	 */
	public ImagePainter(TrainMan trainMan) {
		super();
		this.trainMan = trainMan;
	}

	public void draw(Graphics g, ImageObserver observer, int width, int height) {

		this.width = width;
		this.height = height;
		drawEmpty(g, observer);

		if (!test)
			drawImages(g, observer);

	}

	public void drawImages(Graphics g, ImageObserver observer) {

		int imgRec = 128;
		int tSize;
		Train train = trainMan.getCurrent();

		if (train == null)
			return;
		tSize = train.images.length;

		int gIndex = 0;
		switch (tSize) {
		case 4:
			gIndex = 0;
			break;
		case 6:
			gIndex = 1;
			break;
		case 9:
			gIndex = 2;
			break;
		case 20:
			gIndex = 3;
			break;
		case 30:
			gIndex = 4;
			imgRec = 102;
			break;
		case 40:
			gIndex = 5;
			imgRec = 96;
			break;
		default:
			gIndex = 0;
		}
		int rX = grids[gIndex][0];
		int rY = grids[gIndex][1];

		int gWidth = imgRec * rX + rX + 1;
		int gHeight = imgRec * rY + rY + 1;

		int x = (width / 2) - (gWidth / 2);
		int y = (height / 2) - (gHeight / 2);

		if (x < 0) {
			x = 0;
		}
		if (y < 0) {
			y = 0;
		}
		g.setColor(Color.black);
		if (train.buffers == null) {// 判断是否有建立缓存，如果没有，则直接读取磁盘中的图片
			train.buffers = new BufferedImage[train.images.length];
			for (int iX = 0, count = 0; iX < rX; iX++) {
				int theX = x + (imgRec + 1) * iX + 1;
				for (int iY = 0; iY < rY; iY++, count++) {
					int theY = y + (imgRec + 1) * iY + 1;
					BufferedImage bImg = null;
					String imgPath = trainMan.getImagesDir() + File.separator
							+ train.images[count];
					try {
						bImg = ImageIO.read(new File(imgPath));
						bImg = scalImage(bImg, imgRec);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					train.buffers[count] = bImg;
					g.drawImage(bImg, theX, theY, observer);

				}

			}
		} else {
			for (int iX = 0, count = 0; iX < rX; iX++) {
				int theX = x + (imgRec + 1) * iX + 1;
				for (int iY = 0; iY < rY; iY++, count++) {
					int theY = y + (imgRec + 1) * iY + 1;

					g.drawImage(train.buffers[count], theX, theY, observer);

				}

			}
		}
	}

	protected BufferedImage scalImage(BufferedImage bImg, int imgRec) {
		int width = bImg.getWidth();
		int height = bImg.getHeight();
		if (width <= imgRec && height <= imgRec)
			return bImg;
		System.out.println("scaling image......");
		BufferedImage bdest = new BufferedImage(imgRec, imgRec,
				BufferedImage.TYPE_4BYTE_ABGR);

		Graphics2D g = bdest.createGraphics();

		AffineTransform at = AffineTransform.getScaleInstance((double) imgRec
				/ (double) bImg.getWidth(), (double) imgRec
				/ (double) bImg.getHeight());

		g.drawRenderedImage(bImg, at);

		return bdest;

	}

	public void drawEmpty(Graphics g, ImageObserver observer) {

		int imgRec = 128;
		int tSize = 4;
		Train train = trainMan.getCurrent();
		if (train != null)
			tSize = train.images.length;
		int gIndex = 0;
		switch (tSize) {
		case 4:
			gIndex = 0;
			break;
		case 6:
			gIndex = 1;
			break;
		case 9:
			gIndex = 2;
			break;
		case 20:
			gIndex = 3;
			break;
		case 30:
			gIndex = 4;
			imgRec = 102;
			break;
		case 40:
			gIndex = 5;
			imgRec = 96;
			break;
		default:
			gIndex = 0;
		}
		int rX = grids[gIndex][0];
		int rY = grids[gIndex][1];

		int gWidth = imgRec * rX + rX + 1;
		int gHeight = imgRec * rY + rY + 1;

		g.setColor(Color.white);
		g.fillRect(0, 0, width, height);
		int x = (width / 2) - (gWidth / 2);
		int y = (height / 2) - (gHeight / 2);

		if (x < 0) {
			x = 0;
		}
		if (y < 0) {
			y = 0;
		}
		g.setColor(Color.LIGHT_GRAY);
		for (int iX = 0; iX < rX; iX++) {
			int theX = x + (imgRec + 1) * iX;
			for (int iY = 0; iY < rY; iY++) {
				int theY = y + (imgRec + 1) * iY;
				g.drawRect(theX, theY, imgRec + 2, imgRec + 2);
			}

		}
	}
	
	
}
